﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RPGXEngine.Factories;

namespace RPGXEngine
{
    public class Script
    {
        #region Properties

        public string FileName { get; private set; }

        public ScriptDataTree DataTree
        {
            get { return _dataTree; }
            set { _dataTree = value; }
        }
        private ScriptDataTree _dataTree = new ScriptDataTree();

        public ILogger Logger
        {
            get { return _logger; }
            set { _logger = value; }
        }
        private ILogger _logger = null;

        #endregion

        #region Public Methods

        public Script()
        {
            this.Logger = LoggerFactory.Get();
        }

        public bool Load(string filename, ScriptManager sm)
        {
            try
            {
                FileStream fs = File.OpenRead(filename);

                StreamReader reader = new StreamReader(fs);

                string script = reader.ReadToEnd();

                fs.Close();

                this.FileName = filename;

                ScriptParser parser = new ScriptParser();
                this.DataTree = parser.Parse(script, sm);
                this.DataTree.AddressFunctions(new List<Script>() { this });
            }
            catch (Exception)
            {
                this.Logger.Log(String.Format("Failed to open script file: {0}", filename));

                return false;
            }

            return true;
        }

        public void UnLoad()
        {
        }

        #endregion
    }
}
